<?php
/**
 * Created by PhpStorm.
 * User: Shen
 * Date: 2016/7/24
 * Time: 18:18
 */
namespace DuiCode;

class AllUser implements \Iterator{

    protected $ids;
    protected $data = array();
    protected $index;

    function __construct(){
        $db = Factory::getDatabase();
        $result = $db->query("SELECT id FROM user");
        $this->ids = $result->fetch_all(MYSQLI_ASSOC);

    }

    //获取当前的元素（第三个调用）
    function current(){
        // TODO: Implement current() method.
        $id = $this->ids[$this->index]['id'];
        return Factory::getUser($id);
    }

    //下一个元素(第四步)
    function next(){
        // TODO: Implement next() method.
        $this->index ++;
    }

    //验证当前是否还有下一个元素（第二个调用）
    function valid(){
        // TODO: Implement valid() method.
        return $this->index < count($this->ids);
    }

    //重置整个迭代器
    function rewind(){
        // TODO: Implement rewind() method.
        return $this->index;
    }

    //在迭代器中的位置(第一个调用)
    function key(){
        // TODO: Implement key() method.
        $this->index = 0;
    }
}